package com.jml.security.login.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.jml.security.login.SessionSocialUser;
import com.jml.security.properties.SecurityProperties;
import com.jml.util.entity.SimpleResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;


@Controller
public class BrowserSecurityController {

	private Logger logger = LoggerFactory.getLogger(getClass());

	//获取是html页面跳转过来的还是内部跳转的
	private RequestCache requestCache = new HttpSessionRequestCache();

	private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

	@Autowired
	private SecurityProperties securityProperties;


	 //* 当需要身份认证时，跳转到这里
	@RequestMapping("/authentication/require")
	//@ResponseStatus(code = HttpStatus.UNAUTHORIZED) //返回未授权的状态码
	public String requireAuthentication(HttpServletRequest request, HttpServletResponse response)throws Exception {
		SavedRequest savedRequest = requestCache.getRequest(request, response);
		logger.info("引发跳转的请求是:" + securityProperties.getBrowser().getLoginPage());
		return securityProperties.getBrowser().getLoginPage();
		/*if (savedRequest != null) {
			String targetUrl = savedRequest.getRedirectUrl();
			logger.info("引发跳转的请求是:" + targetUrl);
			if (StringUtils.endsWithIgnoreCase(targetUrl, ".html")) {
				String prourl = securityProperties.getBrowser().getLoginPage();
				redirectStrategy.sendRedirect(request, response, securityProperties.getBrowser().getLoginPage());
			}
		}*/
		//return new SimpleResponse("please go to login page 访问的服务需要身份认证，请引导用户到登录页");
	}

	@RequestMapping("/session/invalid")
	@ResponseBody
	@ResponseStatus(code=HttpStatus.UNAUTHORIZED)
	public SimpleResponse sessionInvalid(HttpServletRequest request, HttpServletResponse response)throws Exception {
		String message = "session invalid 失效";
		return new SimpleResponse(message);
	}


	@RequestMapping("/getRole")
	@ResponseBody
	public String getRole(HttpServletRequest request, HttpServletResponse response)throws Exception {
		ObjectMapper objectMapper =new ObjectMapper();
		SessionSocialUser principal = (SessionSocialUser) SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		return objectMapper.writeValueAsString(principal);
	}

	/*@GetMapping("/social/user")
	public SocialUserInfo getSocialUserInfo(HttpServletRequest request) {
		SocialUserInfo userInfo = new SocialUserInfo();
		Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
		userInfo.setProviderId(connection.getKey().getProviderId());
		userInfo.setProviderUserId(connection.getKey().getProviderUserId());
		userInfo.setNickname(connection.getDisplayName());
		userInfo.setHeadimg(connection.getImageUrl());
		return userInfo;
	}*/

}
